package com.zengguang.testkaoshi.services.impl;

import com.zengguang.testkaoshi.dao.StudentInfoDao;
import com.zengguang.testkaoshi.pojo.StudentInfo;
import com.zengguang.testkaoshi.services.IStudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentInfoService implements IStudentInfoService {
    @Autowired
    private StudentInfoDao studentInfoDao;
    @Override
    public int insertStudentInfo(StudentInfo studentInfo) {
        int returnInt=0;
        /*
         * 1.业务判断
         * (1)学生id不能重复
         */
        //(2)判断角色
        int studentIDCount=studentInfoDao.getStudentIDCount(studentInfo.getStudentinfoid());
        if (studentIDCount<1){
            //2、数据添加
            int addClassInt=studentInfoDao.insertStudentInfo(studentInfo);
            //3、返回值设置
            returnInt=addClassInt;
        }else {
            returnInt=-1;//表示有相同id,添加失败
        }

        return returnInt;
    }
    //修改学生信息
    @Override
    public int updateStudent(StudentInfo studentInfo) {
        int returnInt=0;
        /*
         * 1.业务判断
         * (1)根据学生信息查询
         * （2）学生修改
         */
        int studentIDCount=studentInfoDao.getStudentIDCount(studentInfo.getStudentinfoid());
        if (studentIDCount>0){
            //2、数据修改
            int updateStudentInt=studentInfoDao.updateStudent(studentInfo);
            //3、返回值设置
            returnInt=updateStudentInt;
        }else {
            returnInt=-1;//表示有相同id,添加失败
        }

        return returnInt;
    }
    //学生信息查询
    @Override
    public List<StudentInfo> selectAllStudent() {
        List<StudentInfo> list=studentInfoDao.selectAllStudent();
        return list;
    }

    //根据学生编码删除
    @Override
    public int deleteStudent(int studentinfoid) {
        int temp=0;
        try{
            temp=studentInfoDao.getStudentIDCount(studentinfoid);
            if (temp>0){
                temp=studentInfoDao.deleteStudent(studentinfoid);
            }else{
                return -1;
            }
        }catch (Exception e){
            return -1;
        }
        return temp;
    }
}
